package com.vancouver.knows.ui.activity.userpost;

import android.content.ContentResolver;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.pangu.util.StringUtil;
import com.squareup.picasso.Picasso;
import com.vancouver.knows.R;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * An adapter to display photos that are to be uploaded.
 * Each row in the adapter is a linear layout containing
 * 3 image containers each getItemCount() returns the max
 * amount of rows needed to display at most 3 images in
 * each row.
 *
 * Created by Chris Li on 2016-07-19.
 */
public class PhotosAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private LayoutInflater mLayoutInflater;
    private List<String> mImages;
    private PhotoAdapterListener mListener;
    private int mNewImageStartIndex = -1;

    public PhotosAdapter(Context context) {
        this.mLayoutInflater = LayoutInflater.from(context);
        List<String> images = new ArrayList<>();
        images.add("");
        this.mImages = images;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mLayoutInflater.inflate(R.layout.photo_image_row_layout, parent, false);
        return new ImageViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        final ImageViewHolder imageViewHolder = (ImageViewHolder) holder;
        int startIndex = position * 3;
        final List<String> images = mImages.subList(startIndex, Math.min(startIndex + 3, mImages.size()));

        if (images.size() > 0) {
            imageViewHolder.firstImageContainer.setVisibility(View.VISIBLE);
            imageViewHolder.firstDefaultImage.setVisibility(View.GONE);
            imageViewHolder.firstImageView.setVisibility(View.VISIBLE);
            if (!StringUtil.isEmptyString(images.get(0))) {
                loadImage(imageViewHolder.itemView.getContext(), images.get(0), imageViewHolder.firstImageView);
                final int actualPosition = imageViewHolder.getAdapterPosition() * 3;
                if (actualPosition > mNewImageStartIndex) {
                    imageViewHolder.firstRemoveButton.setVisibility(View.VISIBLE);
                    imageViewHolder.firstRemoveButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mImages.remove(actualPosition);
                            notifyDataSetChanged();
                        }
                    });
                } else {
                    imageViewHolder.firstRemoveButton.setVisibility(View.GONE);
                }
            }
            imageViewHolder.firstImageContainer.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        mListener.onImageClicked(images.get(0));
                    }
                }
            });

            if (images.size() <= 1 && position == getItemCount() - 1) {
                imageViewHolder.firstDefaultImage.setVisibility(View.VISIBLE);
                imageViewHolder.firstRemoveButton.setVisibility(View.GONE);
                imageViewHolder.firstImageView.setVisibility(View.GONE);
                imageViewHolder.firstImageContainer.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mListener != null) {
                            mListener.onAddImageClicked();
                        }
                    }
                });
                imageViewHolder.secondImageContainer.setVisibility(View.INVISIBLE);
                imageViewHolder.thirdImageContainer.setVisibility(View.INVISIBLE);
            }
        }

        if (images.size() > 1) {
            imageViewHolder.secondImageContainer.setVisibility(View.VISIBLE);
            imageViewHolder.secondDefaultImage.setVisibility(View.GONE);
            imageViewHolder.secondImageView.setVisibility(View.VISIBLE);
            if (!StringUtil.isEmptyString(images.get(1))) {
                loadImage(imageViewHolder.itemView.getContext(), images.get(1), imageViewHolder.secondImageView);
                final int actualPosition = (imageViewHolder.getAdapterPosition() * 3) + 1;
                if (actualPosition > mNewImageStartIndex) {
                    imageViewHolder.secondRemoveButton.setVisibility(View.VISIBLE);
                    imageViewHolder.secondRemoveButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mImages.remove(actualPosition);
                            notifyDataSetChanged();
                        }
                    });
                } else {
                    imageViewHolder.secondRemoveButton.setVisibility(View.GONE);
                }
            }
            imageViewHolder.secondImageContainer.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        mListener.onImageClicked(images.get(1));
                    }
                }
            });

            if (images.size() <= 2 && position == getItemCount() - 1) {
                imageViewHolder.secondDefaultImage.setVisibility(View.VISIBLE);
                imageViewHolder.secondImageView.setVisibility(View.GONE);
                imageViewHolder.secondRemoveButton.setVisibility(View.GONE);
                imageViewHolder.secondImageContainer.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mListener != null) {
                            mListener.onAddImageClicked();
                        }
                    }
                });
                imageViewHolder.thirdImageContainer.setVisibility(View.INVISIBLE);
            }
        }

        if (images.size() > 2) {
            imageViewHolder.thirdImageContainer.setVisibility(View.VISIBLE);
            imageViewHolder.thirdDefaultImage.setVisibility(View.GONE);
            imageViewHolder.thirdImageView.setVisibility(View.VISIBLE);
            if (!StringUtil.isEmptyString(images.get(2))) {
                loadImage(imageViewHolder.itemView.getContext(), images.get(2), imageViewHolder.thirdImageView);
                final int actualPosition = (imageViewHolder.getAdapterPosition() * 3) + 2;
                if (actualPosition > mNewImageStartIndex) {
                    imageViewHolder.thirdRemoveButton.setVisibility(View.VISIBLE);
                    imageViewHolder.thirdRemoveButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mImages.remove(actualPosition);
                            notifyDataSetChanged();
                        }
                    });
                } else {
                    imageViewHolder.thirdRemoveButton.setVisibility(View.GONE);
                }
            }
            imageViewHolder.thirdImageContainer.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        mListener.onImageClicked(images.get(2));
                    }
                }
            });

            if (images.size() == 3 && position == getItemCount() - 1) {
                imageViewHolder.thirdDefaultImage.setVisibility(View.VISIBLE);
                imageViewHolder.thirdImageView.setVisibility(View.GONE);
                imageViewHolder.thirdRemoveButton.setVisibility(View.GONE);
                imageViewHolder.thirdImageContainer.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mListener != null) {
                            mListener.onAddImageClicked();
                        }
                    }
                });
            }
        }
    }

    private void loadImage(Context context, String path, ImageView imageView) {
        if (path.startsWith(ContentResolver.SCHEME_CONTENT)) {
            Picasso.with(context).load(path).centerCrop()
                    .resizeDimen(R.dimen.image_size, R.dimen.image_size)
                    .into(imageView);

        } else {
            File file = new File(path);
            if (file.exists()) {
                Picasso.with(context).load(file).centerCrop()
                        .resizeDimen(R.dimen.image_size, R.dimen.image_size)
                        .into(imageView);
            } else {
                Picasso.with(context).load(path).centerCrop()
                        .resizeDimen(R.dimen.image_size, R.dimen.image_size)
                        .into(imageView);
            }
        }
    }

    public List<String> getImages() {

        if (mNewImageStartIndex != -1) {
            return mImages.subList(mNewImageStartIndex + 1, mImages.size() - 1);
        }

        return mImages.subList(0, mImages.size() - 1);
    }

    public void setListener(PhotoAdapterListener listener) {
        this.mListener = listener;
    }

    @Override
    public int getItemCount() {
        return (mImages.size() + 3 - 1) / 3; // round up
    }

    public void addImage(String path) {
        mImages.add(mImages.size() - 1, path);
        notifyDataSetChanged();
    }

    public void setImages(List<String> images) {
        mImages.addAll(0, images);
        notifyDataSetChanged();
        mNewImageStartIndex = mImages.size() - 2;
    }

    public void setNewImageStartIndex() {
        mNewImageStartIndex = mImages.size() - 2;
        notifyDataSetChanged();
    }

    public void clear() {
        mImages.clear();
        mImages.add("");
        notifyDataSetChanged();
    }

    public static class ImageViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.first_image)
        RelativeLayout firstImageContainer;
        @Bind(R.id.second_image)
        RelativeLayout secondImageContainer;
        @Bind(R.id.third_image)
        RelativeLayout thirdImageContainer;

        LinearLayout firstDefaultImage;
        LinearLayout secondDefaultImage;
        LinearLayout thirdDefaultImage;

        ImageView firstImageView;
        ImageView secondImageView;
        ImageView thirdImageView;

        ImageView firstRemoveButton;
        ImageView secondRemoveButton;
        ImageView thirdRemoveButton;

        public ImageViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            firstImageContainer.setVisibility(View.INVISIBLE);
            firstDefaultImage = (LinearLayout) firstImageContainer.findViewById(R.id.default_image);
            firstImageView = (ImageView) firstImageContainer.findViewById(R.id.photo);
            firstRemoveButton = (ImageView) firstImageContainer.findViewById(R.id.remove_button);

            secondImageContainer.setVisibility(View.INVISIBLE);
            secondDefaultImage = (LinearLayout) secondImageContainer.findViewById(R.id.default_image);
            secondImageView = (ImageView) secondImageContainer.findViewById(R.id.photo);
            secondRemoveButton = (ImageView) secondImageContainer.findViewById(R.id.remove_button);

            thirdImageContainer.setVisibility(View.INVISIBLE);
            thirdDefaultImage = (LinearLayout) thirdImageContainer.findViewById(R.id.default_image);
            thirdImageView = (ImageView) thirdImageContainer.findViewById(R.id.photo);
            thirdRemoveButton = (ImageView) thirdImageContainer.findViewById(R.id.remove_button);
        }
    }

    public interface PhotoAdapterListener {
        void onAddImageClicked();
        void onImageClicked(String imagePath);
    }

}